package de.fhluebeck.oop.gps.gui;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JOptionPane;

import de.fhluebeck.oop.gps.common.Constants;
import de.fhluebeck.oop.gps.common.DependencyManager;
import de.fhluebeck.oop.gps.entity.Category;
import de.fhluebeck.oop.gps.service.ICategoryService;

/**
 * 
 * Beschreibung:<br>Controlls für den KatagorieView.
 * @author Ogün Bilge, Leif Hitzschke
 * @version 1.00, 05.11.2009
 * 
 */
public class KategorieController implements ActionListener, MouseListener {

	KategorieView view = null;
	Category selectedCategory;
	
    private ICategoryService categoryService = (ICategoryService) DependencyManager.locate(Constants.CATEGORY_SERVICE_DEP_NAME);
	
    private FrameCloseListener frameCloseListener;
    
	public KategorieController(KategorieView kategorieView, FrameCloseListener frameCloseListener) {
		view = kategorieView;
		this.frameCloseListener = frameCloseListener ;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
				
		if(e.getActionCommand() == "cmdCancel") view.dispose();
		
		if(e.getActionCommand() == "cmdSave") {
			
			// Wenn das selectedCategoryObjekt leer ist, handelt es sich um einen
			// neuen Datensatz
			if(selectedCategory != null) {
				
				selectedCategory.setName(view.name.getText());
				selectedCategory.setDescription(view.description.getText());
				
				if(this.validate(selectedCategory)) {
					categoryService.update(selectedCategory);
				}
				else {
					JOptionPane.showMessageDialog(view, "Bitte geben Sie eine Bezeichnung und einen Text ein.");
				}
			}
			else {
				
				selectedCategory = new Category();
				selectedCategory.setName(view.name.getText());
				selectedCategory.setDescription(view.description.getText());
				
				if(this.validate(selectedCategory)) {
					categoryService.createCategory(selectedCategory);
				}
				else {
					JOptionPane.showMessageDialog(view, "Bitte geben Sie eine Bezeichnung und einen Text ein.");
				}
			}
			this.updateListOfCategories();
			emptyFields();
			frameCloseListener.updateUI();
			selectedCategory = null;
		}
		
		if(e.getActionCommand() == "cmdNew") {
			selectedCategory = null;
			emptyFields();
		}
		
		if(e.getActionCommand() == "cmdDelete") {
			categoryService.delete(selectedCategory);
			selectedCategory = null;
			emptyFields();
			this.updateListOfCategories();
			frameCloseListener.updateUI();
		}

	}
	
	private boolean validate(Category category) {
		
		if(category.getName().isEmpty() && category.getDescription().isEmpty()) {
			return false;
		}
		return true;
	}
	
	/***
	 * Leert die Felder f�r das Kategoriefeld.
	 */
	private void emptyFields() {
		view.name.setText("");
		view.description.setText("");
	}
	
	/**
	 * L�dt die Daten f�r die Tabelle.
	 */
	private  void updateListOfCategories() {
		view.uebersicht.setModel(new CategoryTableModel());
	}
	
	void initializeFields() {
		updateListOfCategories();
	}
	
	/**
	 * Ermittelt die angeklickte Zeile und dann das zu bearbeitende Categoryobjekt.
	 */
	@Override
	public void mouseClicked(MouseEvent e) {
		
		
			Point p = e.getPoint();
			int selectedRow = view.uebersicht.rowAtPoint(p);
			
			if(selectedRow >= 0 ) {
				selectedCategory = new Category();
				selectedCategory.setId(Long.valueOf(view.uebersicht.getValueAt(selectedRow,0).toString()));
				selectedCategory.setName(view.uebersicht.getValueAt(selectedRow,1).toString());
				selectedCategory.setDescription(view.uebersicht.getValueAt(selectedRow,2).toString());
			}
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseReleased(MouseEvent me) {
		
		Point p = me.getPoint();
		int selectedRow = view.uebersicht.rowAtPoint(p);
		
		if(selectedRow >= 0 ) {
			
			selectedCategory = new Category();
			selectedCategory.setId(Long.valueOf(view.uebersicht.getValueAt(selectedRow,0).toString()));
			selectedCategory.setName(view.uebersicht.getValueAt(selectedRow,1).toString());
			selectedCategory.setDescription(view.uebersicht.getValueAt(selectedRow,2).toString());
			
			// Nur wenn linke Maustaste gedr�ckt wurde, soll die Bezeichnung und der Text gef�llt werden.
			// Rechte Maustatse = 16
			if(me.getModifiers()== 16) {
				view.name.setText(selectedCategory.getName());	
				view.description.setText(selectedCategory.getDescription());
			}
		}
		
		if(me.isPopupTrigger()) {
			view.popmenu.show(me.getComponent(), me.getX(), me.getY());
		}		
	}
}
